/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entity.Statistique;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Mély
 */
@Stateless
public class StatistiqueFacade extends AbstractFacade<Statistique> implements StatistiqueFacadeLocal {
    @PersistenceContext(unitName = "WebSgbd-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public StatistiqueFacade() {
        super(Statistique.class);
    }
    
    @Override
    public List<Statistique> findAllByFilter(String userid, String type, String compte, Boolean success, String server) {
        
        String sql = "SELECT Object(s) "
                + "FROM Statistique s "
                + "WHERE s.compte.name LIKE :compte "
                + "AND s.utilisateur.userid LIKE :userid "
                + "AND s.compte.typeBDDid.typeBDD LIKE :type "
                + "AND s.compte.server LIKE :server ";
                
        if(success != null) {
            sql += "AND s.success = " + success;        
        }
               // + "AND s.success = " + success;        
        Query query = em.createQuery(sql);
        query.setParameter("compte", "%" + compte + "%");
        query.setParameter("userid", "%" + userid + "%");
        query.setParameter("type", "%" + type + "%");
        query.setParameter("server", "%" + server + "%");
        //query.setParameter("success", success);
        
        return query.getResultList();
    }
    
}
